package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.dto.pk.InsertPkDto;
import com.hbx.dto.pk.SearchDto;
import com.hbx.service.PkService;
import com.hbx.utils.Result;
import com.hbx.vo.res.MyResVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/pk")
@Api(tags = "PK处理器")
public class PkController {
    @Autowired
    private PkService pkService;

    /**
     * 发布pk
     * @param dto
     * @param result
     * @return
     */
    @RequestMapping(value = "/push",method = RequestMethod.POST)
    @ApiOperation("发布pk")
    @SensitiveFilter
    public Result pushPk(@RequestBody @Valid InsertPkDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        pkService.insertPk(dto, r);
        return r;
    }

    /**
     * 获取pk列表
     * @param dto
     * @return
     */
    @RequestMapping(value = "/getPkList",method = RequestMethod.POST)
    @ApiOperation("获取pk列表")
    public Result getPkList(@RequestBody SearchDto dto){
        Result r = new Result();
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        if (pageNum == null || pageNum <= 0){
            r.setCode(400).setMsg("入参错误");
            return r;
        }
        if (pageSize == null || pageSize <= 0){
            r.setCode(400).setMsg("入参错误");
            return r;
        }
        pkService.searchList(dto, r);
        return r;
    }

    /**
     * 查询我的Pk
     * @param pageSize
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getMyPk",method = RequestMethod.GET)
    @ApiOperation("查询我的Pk")
    public Result<MyResVo> getMyPk(@RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("userId") Integer userId){
        Result<MyResVo> r = new Result<>();
        if (ObjectUtils.isEmpty(pageSize)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("查询条数为必传项");
            return r;
        }
        if (ObjectUtils.isEmpty(userId)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("userId为必传项");
            return r;
        }
        pkService.getMyPk(pageSize, userId ,r);
        return r;
    }

    /**
     * 删除我的Pk
     * @param id
     * @return
     */
    @RequestMapping(value = "/removePk",method = RequestMethod.GET)
    @ApiOperation("删除我的Pk")
    public Result removeRes(@RequestParam("id") Integer id){
        Result r = new Result<>();
        if (ObjectUtils.isEmpty(id)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("id为必传项");
            return r;
        }
        pkService.removePk(id, r);
        return r;
    }

    /**
     * 获得Pk详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/getPkInfo",method = RequestMethod.GET)
    @ApiOperation("获得Pk详情")
    public Result getPkInfo(@RequestParam("id") Integer id){
        Result r = new Result<>();
        if (ObjectUtils.isEmpty(id)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("id为必传项");
            return r;
        }
        pkService.getPkInfo(id, r);
        return r;
    }
}

